/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package syncmellariusnote;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.CommPortIdentifier;
import javax.comm.ParallelPort;

/**
 *
 * @author Vitor
 */


public class Sender {

    public final static int FILE_SIZE = 6358176;
    int bytesRead;
    int current = 0;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    String FILE_TO_SEND;
    int baseAddress = 0x378;
    //LPT Port
    static Enumeration<?> portList;
    static CommPortIdentifier portId;
    ParallelPort parallelPort;

    public void sendFile(Socket sock,String filePath ) {
        try {
            FILE_TO_SEND = filePath;
            DataOutputStream os = new DataOutputStream(sock.getOutputStream())  ;
            File myFile = new File(FILE_TO_SEND);
            int length;
            byte[] mybytearray = new byte[(int) myFile.length()];
            byte[] buffer = new byte[1024];
        
            fis = new FileInputStream(myFile);
            bis = new BufferedInputStream(fis);
            
            
            os.writeInt((int)myFile.length());
            os.flush();
            while ((length = bis.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
            System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
            fis.close();
            bis.close();
            os.close();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Receptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Receptor.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
}
